<template>
  <el-form
    ref="ruleForm"
    :model="ruleForm"
    :rules="rules"
    label-width="120px"
    size="small"
  >
    <el-form-item label="供应商名称" prop="supplierName">
      <el-input v-model.trim="ruleForm.supplierName" placeholder="请输入供应商名称"></el-input>
    </el-form-item>

    <el-form-item label="供应商联系人" prop="supplierContactName">
      <el-input v-model.trim="ruleForm.supplierContactName" placeholder="请输入供应商联系人"></el-input>
    </el-form-item>

    <el-form-item label="供应商联系电话" prop="supplierContactNumber">
      <el-input v-model.trim="ruleForm.supplierContactNumber" placeholder="请输入供应商联系电话"></el-input>
    </el-form-item>
  </el-form>
</template>

<script>
/* Mixins
------------------------------- */
import { Form } from '@/mixins'

/* Api
------------------------------- */
import { checkSupplierFreshName, checkSupplierFreshContactNumber } from '@/api'

/* Model
------------------------------- */
import { supplierFreshModel, primaryKey } from '../model'

export default {
  mixins: [Form('ruleForm', supplierFreshModel)],
  data () {
    const asyncCheckSupplierFreshName = (rule, value, callback) => {
      return this.checkSupplierFreshName(
        {
          name: value,
          id: this.ruleForm[primaryKey]
        },
        {
          onOk ({ data }) {
            !data ? callback(new Error('当前供应商名称已存在')) : callback()
          },
          onError ({ message }) {
            callback(new Error(message))
          }
        }
      )
    }

    const asyncCheckSupplierFreshContactNumber = (rule, value, callback) => {
      return this.checkSupplierFreshContactNumber(
        {
          number: value,
          id: this.ruleForm[primaryKey]
        },
        {
          onOk ({ data }) {
            !data
              ? callback(new Error('当前供应商联系电话已存在'))
              : callback()
          },
          onError ({ message }) {
            callback(new Error(message))
          }
        }
      )
    }

    return {
      ruleForm: Object.assign({}, supplierFreshModel),
      rules: {
        supplierName: [
          {
            required: true,
            message: this.$t('required', { field: '供应商名称' })
          },
          { validator: asyncCheckSupplierFreshName, trigger: 'blur' }
        ],
        supplierContactName: [
          {
            required: true,
            message: this.$t('required', { field: '供应商联系人' })
          }
        ],
        supplierContactNumber: [
          {
            required: true,
            message: this.$t('required', { field: '供应商联系电话' })
          },
          { validator: asyncCheckSupplierFreshContactNumber, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // --------------------------------------  request --------------------------------------
    checkSupplierFreshName ({ name, id }, { onOk, onError }) {
      return this.Catch(checkSupplierFreshName, {
        onOk,
        onError
      })(null, name, id)
    },

    checkSupplierFreshContactNumber ({ number, id }, { onOk, onError }) {
      return this.Catch(checkSupplierFreshContactNumber, {
        onOk,
        onError
      })(null, number, id)
    }

    // -------------------------------------- !request --------------------------------------
  }
}
</script>
